﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PostSharp4ViewState.Tests
{
   public class BaseClass : BaseControl
   {
      [Persist(Mode = PersistMode.ViewState)]
      public string StringValue;

      [Persist(Mode = PersistMode.ViewState)]
      public int IntValue;

      [Persist(Mode = PersistMode.ControlState)]
      public string StringCSValue;

      [Persist(Mode = PersistMode.ControlState)]
      public int IntCSValue;            
   }

   public class InheritedClass : BaseClass
   {
      [Persist(Mode = PersistMode.ViewState)]
      public string NewStringValue;

      [Persist(Mode = PersistMode.ViewState)]
      public int NewIntValue;

      [Persist(Mode = PersistMode.ControlState)]
      public string NewStringCSValue;

      [Persist(Mode = PersistMode.ControlState)]
      public int NewIntCSValue;
   }   

   [TestClass]
   public class InheritanceTests
   {
      [TestMethod]
      public void InheritedLoadControlStateMatchesSaveControlState()
      {
         InheritedClass o = new InheritedClass();
         o.StringCSValue = "value";
         o.IntCSValue = 16;
         o.NewStringCSValue = "newValue";
         o.NewIntCSValue = 4;
         InheritedClass newO = new InheritedClass();
         newO.DoLoadControlState(o.DoSaveControlState());

         Assert.AreEqual("value", newO.StringCSValue);
         Assert.AreEqual(16, newO.IntCSValue);
         Assert.AreEqual("newValue", newO.NewStringCSValue);
         Assert.AreEqual(4, newO.NewIntCSValue);
      }
      [TestMethod]
      public void InheritedLoadControlStateWorks()
      {
         InheritedClass o = new InheritedClass();
         o.DoLoadControlState(new object[] { new object[] { null, new object[] { 15, "baseValue" } }, new object[] { 4, "value" } });
         Assert.AreEqual("baseValue", o.StringCSValue);
         Assert.AreEqual(15, o.IntCSValue);
         Assert.AreEqual("value", o.NewStringCSValue);
         Assert.AreEqual(4, o.NewIntCSValue);
      }
      [TestMethod]
      public void InheritedSaveControlStateWorks()
      {
         InheritedClass o = new InheritedClass();
         o.IntCSValue = 15;
         o.StringCSValue = "baseValue";
         o.NewIntCSValue = 4;
         o.NewStringCSValue = "value";
         object[] state = (object[])o.DoSaveControlState();
         
         Assert.IsInstanceOfType(state[1], typeof(object[]));
         object[] newState = (object[])state[1];
         Assert.AreEqual(newState[0], 4);
         Assert.AreEqual(newState[1], "value");

         state = (object[])state[0];

         Assert.IsInstanceOfType(state[1], typeof(object[]));
         object[] oldState = (object[])state[1];
         Assert.AreEqual(oldState[0], 15);
         Assert.AreEqual(oldState[1], "baseValue");
      }

      [TestMethod]
      public void InheritedLoadViewStateWorks()
      {
         InheritedClass o = new InheritedClass();
         o.DoLoadViewState(new object[] { new object[] { null, new object[] { 15, "baseValue" } }, new object[] { 4, "value" } });
         Assert.AreEqual("baseValue", o.StringValue);
         Assert.AreEqual(15, o.IntValue);
         Assert.AreEqual("value", o.NewStringValue);
         Assert.AreEqual(4, o.NewIntValue);
      }
      [TestMethod]
      public void InheritedSaveViewStateWorks()
      {
         InheritedClass o = new InheritedClass();
         o.IntValue = 15;
         o.StringValue = "baseValue";
         o.NewIntValue = 4;
         o.NewStringValue = "value";
         object[] state = (object[])o.DoSaveViewState();

         Assert.IsInstanceOfType(state[1], typeof(object[]));
         object[] newState = (object[])state[1];
         Assert.AreEqual(newState[0], 4);
         Assert.AreEqual(newState[1], "value");

         state = (object[])state[0];

         Assert.IsInstanceOfType(state[1], typeof(object[]));
         object[] oldState = (object[])state[1];
         Assert.AreEqual(oldState[0], 15);
         Assert.AreEqual(oldState[1], "baseValue");
      }
   }
}
